package com.agu.utils;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.lang.RandomStringUtils;

/**
 * 网关常用函数帮助类
 * 
 * @author Sansan
 *
 */
public class IsmsUtils {
	
	/**
	 * 字符串转换UTF-16编码
	 * @param str 
	 * @return
	 */
	public static String toUtf16(String str) {
		String value = "";
		if (str != null && str.length() > 0) {
			byte[] b;
			try {
				b = str.getBytes("UTF-16");
				byte[] bb = new byte[b.length - 2];
				System.arraycopy(b, 2, bb, 0, b.length - 2);
				value = getHexStr(bb);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return value;
	}

	// 取字节的16进制字符串
	public static String getHexStr(byte[] bs) {
		String retStr = "";
		for (int i = 0; i < bs.length; i++) {
			if (Integer.toHexString((int) bs[i]).length() > 1) {
				retStr += Integer.toHexString((int) bs[i]).substring(Integer.toHexString((int) bs[i]).length() - 2);
			} else {
				retStr += "0"
						+ Integer.toHexString((int) bs[i]).substring(Integer.toHexString((int) bs[i]).length() - 1);
				;
			}
		}
		return retStr;
	}

	// 16进制字符串转字节数组
	public static byte[] getByte(String byteStr) {
		if (byteStr.length() % 2 != 0) {
			byteStr = "0" + byteStr;
		}
		byte[] retByte = new byte[byteStr.length() / 2];

		// System.out.println(byteStr.length()/2);
		for (int i = 0; i < byteStr.length() / 2; i++) {
			// System.out.println(i);
			byte[] b = new byte[1];
			b[0] = toByte(Integer.parseInt(byteStr.substring(2 * i, 2 * i + 2), 16))[3];

			retByte[i] = toByte(Integer.parseInt(byteStr.substring(2 * i, 2 * i + 2), 16))[3];
		}
		return retByte;
	}

	// 取int的16进制字符串
	public static String toHexStr(int n) {
		String tempStr = Integer.toHexString(n);
		int j = 8 - tempStr.length();
		for (int i = 0; i < j; i++) {
			tempStr = "0" + tempStr;
		}
		return "0x" + tempStr;
	}

	public static String toHexStr(String n) {
		String tempStr = Integer.toHexString(Integer.valueOf(n));
		int j = 8 - tempStr.length();
		for (int i = 0; i < j; i++) {
			tempStr = "0" + tempStr;
		}
		return "0x" + tempStr;
	}

	public static String toHexStr(Long n) {
		System.out.println("toHexStrtoHexStr");
		String tempStr = Long.toHexString(n);
		System.out.println("toHexStrtoHexStr" + n);
		int j = 8 - tempStr.length();
		for (int i = 0; i < j; i++) {
			tempStr = "0" + tempStr;
		}
		return "0x" + tempStr;
	}

	// 取short的16进制字符串
	public static String toHexStr(short n) {
		String tempStr = Integer.toHexString(n);
		int j = 4 - tempStr.length();
		for (int i = 0; i < j; i++) {
			tempStr = "0" + tempStr;
		}
		return "0x" + tempStr;
	}

	// 整数转时间字符串 字符串格式 yyyy-MM-dd HH:mm:ss
	// n为倍数 1 1000 1000000 1/1000
	
	public static String getDateStr(long timeLong, int n) {
		String StrDate = "";
		if (timeLong != 0) {
			System.out.println("timeLong=" + timeLong);
			java.text.SimpleDateFormat obj_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			java.util.Date obj_EndDate = new java.util.Date(timeLong * n);
			StrDate = obj_sdf.format(obj_EndDate);
			System.out.println("StrDate=" + StrDate);
		}
		return StrDate;
	}

	// 单字节转short
	public static short toShort(byte b) {
		short si = 0;
		byte[] bs = new byte[2];
		bs[0] = 0;
		bs[1] = b;
		return bytes2short(bs);
	}

	// char转字节
	public static byte[] toByte(char i) {

		byte mybytes[] = new byte[1];
		mybytes[0] = (byte) i;
		return mybytes;
	}

	public static byte[] toByteInt8(char i) {

		byte mybytes[] = new byte[1];
		mybytes[0] = (byte) (i - 48);
		return mybytes;
	}

	// int转字节
	public static byte[] toByte(int i) {

		byte mybytes[] = new byte[4];
		mybytes[3] = (byte) (0xff & i);
		mybytes[2] = (byte) ((0xff00 & i) >> 8);
		mybytes[1] = (byte) ((0xff0000 & i) >> 16);
		mybytes[0] = (byte) ((0xff000000 & i) >> 24);
		return mybytes;
	}

	// short转字节
	public static byte[] toByte(short i) {

		byte mybytes[] = new byte[2];
		mybytes[1] = (byte) (0xff & i);
		mybytes[0] = (byte) ((0xff00 & i) >> 8);
		return mybytes;
	}

	// 读取包中固定位置的int
	// 参数1 包（字节数组）
	// 参数2 起始位置

	// 将byte数组bRefArr转为一个int,字节数组的低位是int的低字节位
	public static int getInt(byte[] bRefArr, int pos) {
		byte[] temp = new byte[4];
		for (int i = 0; i < 4; i++) {
			temp[i] = bRefArr[pos + i];
		}

		return bytes2int(temp);
	}

	// 将byte数组bRefArr转为一个int,字节数组的低位是int的低字节位 根据 bytepos来取
	public static int getLInt(byte[] bRefArr, int pos, int bytepos) {
		byte[] temp = new byte[4];

		for (int i = 0; i < bytepos; i++) {
			if (bytepos > 0) {
				temp[i + (4 - bytepos)] = bRefArr[pos + (4 - bytepos) + i];
			}

		}

		return bytes2int(temp);
	}

	// 将byte数组bRefArr转为一个int,字节数组的低位是int的高字节位 根据 bytepos来取
	public static int getHInt(byte[] bRefArr, int pos, int bytepos) {
		byte[] temp = new byte[4];

		for (int i = 0; i < bytepos; i++) {
			if (bytepos > 0) {
				temp[i + (4 - bytepos)] = bRefArr[pos + i];
			}

		}

		return bytes2int(temp);
	}

	// 读取包中固定位置的short
	// 参数1 包（字节数组）
	// 参数2 起始位置

	// 将byte数组bRefArr转为一个short,字节数组的低位是short的低字节位
	public static short getShort(byte[] bRefArr, int pos) {
		byte[] temp = new byte[2];
		for (int i = 0; i < 2; i++) {
			temp[i] = bRefArr[pos + i];
		}

		return bytes2short(temp);
	}

	public static byte[] getByte(byte[] bRefArr, int pos) {
		byte[] temp = new byte[2];
		for (int i = 0; i < 2; i++) {
			temp[i] = bRefArr[pos + i];
		}

		return temp;
	}

	public static char getChar(byte[] bRefArr, int pos) {
		return (char) bRefArr[pos];
	}

	public static byte[] getByte(byte[] bRefArr, int pos, int length) {
		byte[] temp = new byte[length];
		for (int i = 0; i < length; i++) {
			temp[i] = bRefArr[pos + i];
		}

		return temp;
	}

	/**
	 * 2个字节的byte数组转化成short类型
	 *
	 * @param mybytes
	 *            要转化的长度为2的byte数组
	 * @return int
	 */
	public static short bytes2short(byte mybytes[]) {

		short tmp = (short) ((0xff & mybytes[0]) << 8 | 0xff & mybytes[1]);
		return tmp;
	}

	/**
	 * 4个字节的byte数组转化成int类型
	 *
	 * @param mybytes
	 *            要转化的长度为4的byte数组
	 * @return int
	 */
	public static int bytes2int(byte mybytes[]) {

		int tmp = (0xff & mybytes[0]) << 24 | (0xff & mybytes[1]) << 16 | (0xff & mybytes[2]) << 8 | 0xff & mybytes[3];
		return tmp;
	}

	public static String getStr(byte[] bRefArr) {
		int length = 0;
		for (length = 0; length < bRefArr.length; length++) {
			if (bRefArr[length] == 0)
				break;
		}
		byte[] temp = new byte[length];
		for (int i = 0; i < length; i++) {
			temp[i] = bRefArr[i];
		}
		String tempStr = "";
		try {
			tempStr = new String(temp, 0, temp.length, "GBK");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tempStr;
	}
	
	/**
	 * 生成监控请求索引号
	 * @param indexNum
	 * @return
	 * @throws Exception
	 */
	public static String getSerialNumber(int indexNum) throws Exception {
		// 时间序号
		String time = MyDateUtils.format(Calendar.getInstance().getTime(),MyDateUtils.DATE_PATTERN_S);
		String indexStr = "0000";
		// 索引
		if (indexNum >= 0 && indexNum <= 9999) {
			indexStr = indexStr.substring(Integer.toString(indexNum).length())
					+ indexNum;
		} else {
			throw new Exception("索引号必须是4位！");
		}
		// 随机码
		String randomStr=RandomStringUtils.randomNumeric(4);
		String sn = time + indexStr + randomStr;
		if (sn.length() > 30) {
			throw new Exception("索引号生成错误！");
		}
		return sn;
	}

}
